<template>
    <el-dialog class="dialog" :destroy-on-close="true" title="添加员工授权" :visible.sync="isShow" :close-on-click-modal="false"
        @close="onDialogClose">
        <el-form ref="form" label-width="auto" class="form" :model="formData" :rules="rules">
            <el-form-item label="人员名称" prop="staff_sn" class="big-el big-select">
                <div class="box-row">
                    <uxInputTree class="el-select" v-model="formData.section_sn" :selectParams="selectParams"
                        :treeData="departmentData" :treeParams="treeParams" />
                    <el-select v-model="formData.staff_sn" filterable placeholder="请输入关键词" @change="staffChange">
                        <el-option v-for="item in staffData" :key="item.staff_sn" :label="item.user_xm"
                            :value="item.staff_sn"></el-option>
                    </el-select>
                </div>
            </el-form-item>
            <el-form-item label="二维码" v-if="formData.door_qrcode == '1'">
               {{ formData.door_qrcode == '1' ? '开通' : '未开通' }}
            </el-form-item>
            <el-form-item label="卡号"  v-if="formData.door_card !== ''">
                {{formData.door_card}}
            </el-form-item>
            <el-form-item label="临时密码"  v-if="formData.door_pwd !== ''">
                {{formData.door_pwd}}
            </el-form-item>
            <el-form-item label="人脸" prop="face_thumb" v-if="formData.face_thumb !== ''">
                <el-image style="width: 120px; height: 120px" :src="formData.face_thumb" fit="contain"></el-image>
            </el-form-item>
            <el-form-item label="有效期" prop="end_time">
                <el-date-picker v-model="formData.end_time" value-format="yyyy-MM-dd" placeholder="请选择有效期"
                    :pickerOptions="pickerOptions"></el-date-picker>
            </el-form-item>
            <el-form-item label="梯控" prop="door_sn">
                <el-select multiple v-model="formData.door_sn" placeholder="请选择授权梯控">
                    <el-option v-for="item in ladderSourceData" :key="item.door_sn" :label="item.door_name"
                        :value="item.door_sn"></el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="onDialogClose">取消</el-button>
            <el-button type="primary" @click="onSubmit">确认</el-button>
        </div>
    </el-dialog>
</template>

<script lang="ts">
import { Component, Vue, Watch } from "vue-property-decorator";
import { Action, Getter, State } from "vuex-class";
import { mixinDialogForm } from "@/mixin/dialog/Form";
import { mixinTime } from "@/mixin/Time";
import { mixinNext } from "@/mixin/Next";
import uxInputTree from "@/components/input/tree";
import { find } from 'lodash';
@Component({
    mixins: [mixinTime, mixinDialogForm, mixinNext],
    components: { uxInputTree }
})
export default class equipmentLadderScripManage extends Vue {

    @Action("equipmentLadderscrip/staffAdd") itemAdd: any;
    @Getter("sharing/cardType") typeData: any;
    @Action('equipmentLadder/list') getLadder: any;
    @Action("purviewStaff/list") getStaff: any;
    @Getter("purviewDepartment/tree") departmentData: any;
    @State((state) => state.sharing.func) qrCodeData: any;
    // 表单数据
    formData: any = {
        section_sn: "",
        staff_sn: "",
        door_card: "",
        door_pwd: '',
        face_thumb: '',
        door_qrcode: "",
        end_time: "",
        door_sn: []
    };

    selectParams: object = {
        clearable: true,
        placeholder: "请选择所属部门"
    };

    created() {
        this.remoteMethod();
    }
    staffChange(staff_sn: any) {
        const me = this as any;
        const item = find(this.staffData, { staff_sn });
        me.formData.door_qrcode = item.door_qrcode.toString();
        me.formData.door_card = item.door_card;
        me.formData.door_pwd = item.door_pwd;
        me.formData.face_thumb = item.face_thumb;
    }

    ladderSourceData: any = [];
    @Action('equipmentLadder/list') ladderDataList: any;
    remoteMethod() {
        const me = this as any;
        me.ladderDataList({ page: 1, per_page: 100 }).then(({ data }) => {
            me.ladderSourceData = data;
        });
    }
    staffData: any = [];
    @Watch("formData.section_sn")
    updateBuilding(section_sn: any) {
        const me = this as any;
        me.updateNext(
            me.getStaff,
            { section_sn },
            "staffData",
            "formData.staff_sn",
            {
                queryName: "staff_sn"
            }
        );
    }


    pickerOptions: any = {
        shortcuts: [
            {
                text: '一个月',
                onClick(picker) {
                    const end = new Date();
                    end.setMonth(end.getMonth() + 1);
                    picker.$emit('pick', end);
                }
            },
            {
                text: '三个月',
                onClick(picker) {
                    const end = new Date();
                    end.setMonth(end.getMonth() + 3);
                    picker.$emit('pick', end);
                }
            },
            {
                text: '六个月',
                onClick(picker) {
                    const end = new Date();
                    end.setMonth(end.getMonth() + 6);
                    picker.$emit('pick', end);
                }
            },
            {
                text: '一年',
                onClick(picker) {
                    const end = new Date();
                    end.setMonth(end.getMonth() + 12);
                    picker.$emit('pick', end);
                }
            }
        ]
    };

    /**
     * 校验配置
     *
     * @type {*}
     * @memberof mixinViewRules
     */
    rules: any = {
        staff_sn: [
            {
                required: true,
                message: "请选择人员名称"
            }
        ],
        qrcode: [
            {
                required: true,
                message: "请选择是否开通二维码"
            }
        ],
        end_time: [
            {
                required: true,
                message: "请选择有效期",
                trigger: "blur"
            }
        ],
        door_sn: [
            {
                required: true,
                message: "请选择梯控"
            }
        ],
        scrip: [
            {
                required: true
            }
        ]
    };

    treeParams: any = {
        props: {
            children: "children",
            label: "section_name",
            value: "section_sn"
        }
    };
}
</script>
